package cn.deschen.search.mysql.handlers.impl;

import cn.deschen.search.constants.OpType;
import cn.deschen.search.index.IndexHandler;
import cn.deschen.search.index.adPlan.AdPlanObject;
import cn.deschen.search.mysql.BinlogRowData;
import cn.deschen.search.mysql.handlers.BinlogRowDataHandler;
import cn.deschen.search.utils.SerializableUtil;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Map;

import static cn.deschen.search.constants.TableConstants.AdPlan.*;


/**
 * @Author hanbin_chen
 * @Description 广告计划处理器
 * @DateTime 2021/6/12
 * @Version V1.0.0
 */
@Component
public class AdPlanHandler implements BinlogRowDataHandler {

    @Override
    public void handlerBinlogRowData(BinlogRowData rowData) {
        for (Map<String, Serializable> fieldValueMap : rowData.getRowDataList()) {
            AdPlanObject planObject = new AdPlanObject();

            fieldValueMap.forEach((k, v) -> {
                switch (k) {
                    case COLUMN_ID:
                        planObject.setId(SerializableUtil.parse2Long(v));
                        break;
                    case COLUMN_USER_ID:
                        planObject.setUserId(SerializableUtil.parse2Long(v));
                        break;
                    case COLUMN_PLAN_NAME:
                        planObject.setPlanName(SerializableUtil.parse2String(v));
                        break;
                    case COLUMN_PLAN_STATUS:
                        planObject.setPlanStatus(SerializableUtil.parse2String(v));
                        break;
                    case COLUMN_START_DATE:
                        planObject.setStartDate(SerializableUtil.parse2LocalDateTime(v));
                        break;
                    case COLUMN_END_DATE:
                        planObject.setEndDate(SerializableUtil.parse2LocalDateTime(v));
                        break;
                    case COLUMN_IS_DEL:
                        planObject.setIsDel(cn.deschen.search.mysql.utils.SerializableUtil.parse2Integer(v));
                        break;
                }
            });

            IndexHandler.handlerLevel2Index(planObject, OpType.to(rowData.getEventType()));
        }

    }
}
